/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package privileges.bean;

import java.sql.SQLException;
import privileges.business.User;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import privileges.business.UserBusiness;
import privileges.business.UserDataModel;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean(name = "tableUser")
@RequestScoped
public class TableUsersBean {
    
    private List<User> users;
    private User selectedUser;
    private UserDataModel mediumUserModel;
    private String message;
    

    public TableUsersBean() {
        message = "Ocurrio un Error";
        users = new ArrayList<>();
        fillList();
        mediumUserModel = new UserDataModel(users);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public UserDataModel getMediumUserModel() {
        return mediumUserModel;
    }

    public void setMediumUserModel(UserDataModel mediumUserModel) {
        this.mediumUserModel = mediumUserModel;
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    private void fillList() {
        FacesContext context = FacesContext.getCurrentInstance();
        users = new UserBusiness().getUsers();
        System.out.println("usuarios" + users);
        if(users.isEmpty() || users == null){
            System.out.println("entra");
            context.addMessage(null, new FacesMessage("Advertencia", "Usted No cuenta con los permisos Necesarios"));
        }
    }  
    public void drop(){
        try {
            new UserBusiness().dropUser(selectedUser.getName(),selectedUser.getHost());
            message = "usuario borrado";
        } catch (SQLException ex) {
            message = ex.getLocalizedMessage();
            ex.printStackTrace();
        }
    }

}
